package com.atguigu.guliguli.product.controller;

import com.atguigu.guliguli.common.utils.PageUtils;
import com.atguigu.guliguli.common.utils.R;
import com.atguigu.guliguli.product.entity.AttrEntity;
import com.atguigu.guliguli.product.entity.AttrGroupEntity;
import com.atguigu.guliguli.product.service.AttrAttrGroupRelationService;
import com.atguigu.guliguli.product.service.AttrGroupService;
import com.atguigu.guliguli.product.service.AttrService;
import com.atguigu.guliguli.product.service.CategoryService;
import com.atguigu.guliguli.product.vo.AttrGroupRelationVo;
import com.atguigu.guliguli.product.vo.AttrGroupWithAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author: 叶子
 * @date: 2021/11/27 08:21
 **/
@RestController
@RequestMapping("/product/attrGroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrGroupRelationService relationService;
    /**
     * /product/attrgroup/{attrgroupId}/attr/relation
     **/
    @GetMapping("/{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable Long attrGroupId) {
        List<AttrEntity> entities = attrService.getRelationAttr(attrGroupId);
        return R.ok().put("data", entities);
    }

    @GetMapping("/{attrGroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrGroupId") Long attrGroupId,
                            @RequestParam Map<String, Object> params) {
        PageUtils page = attrService.getNoRelationAttr(params, attrGroupId);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @GetMapping("/list/{catalogId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catalogId") Long catalogId) {
        PageUtils page = attrGroupService.queryPage(params, catalogId);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catalogId = attrGroup.getAttrGroupId();
        Long[] path = categoryService.findCatalogPath(catalogId);
        attrGroup.setCatalogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 删除
     * //product/attrGroup/attr/relation/delete
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos) {
        attrService.deleteRelation(vos);
        return R.ok();
    }

    @GetMapping("/{catalogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable Long catalogId) {
//        1.查出当前分类下的所有分组
//        2.查出每个树形分组的所有属性
        List<AttrGroupWithAttrVo> vos = attrGroupService.getAttrGroupWithAttrsByCatalogId(catalogId);
        return R.ok().put("data", vos);
    }
//    新增分组与属性关联
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
        relationService.saveBatch(vos);
        return R.ok();
    }

}